//
//  HypnosisView.m
//  BigNerdRanchGuidesC6
//
//  Created by Rafal on 3/31/13.
//  Copyright (c) 2013 Rafal Rebacz. All rights reserved.
//

#import "HypnosisView.h"

@implementation HypnosisView

@synthesize circleColor = _circleColor;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if(self)
    {
        // Set all of the view with a clear background color
        [self setBackgroundColor:[UIColor clearColor]];
        _circleColor = [UIColor redColor];
        
        _boxLayer = [[CALayer alloc] init];
        
        [_boxLayer setBounds:CGRectMake(0.0f, 0.0f, 85.0f, 85.0f)];
        [_boxLayer setPosition:CGPointMake(160.0f, 240.0f)];
        
        UIColor *reddish = [UIColor colorWithRed:1.0f green:0.0f blue:0.0f alpha:0.5f];
        CGColorRef cgReddish = reddish.CGColor;
        [_boxLayer setBackgroundColor:cgReddish];
       
        
        UIImage *layerImg = [UIImage imageNamed:@"RealMadrid.png"];
        CGImageRef layerImgRef = layerImg.CGImage;

        // Put CGImage on the layer
        [_boxLayer setContents:(__bridge id)layerImgRef];
        
        // Inset a image a bit on each side
        [_boxLayer setContentsRect:CGRectMake(-0.1f, -0.1f, 1.2f, 1.2f)];
        
        // Let the image resize to superlayer without changing its aspect ratio
        [_boxLayer setContentsGravity:kCAGravityResizeAspect];
        
        [[self layer] addSublayer:_boxLayer];
        
        CALayer *sublayer = [[CALayer alloc] init];
        [sublayer setMasksToBounds:YES];
        [sublayer setCornerRadius:5.f];
        
        UIColor *blue = [UIColor colorWithRed:0.0f green:0.0f blue:1.0f alpha:0.3f];
        CGColorRef cgBlue = blue.CGColor;
        [sublayer setBackgroundColor:cgBlue];
        [sublayer setBounds:CGRectMake(0.0f, 0.0f, _boxLayer.bounds.size.width/2, _boxLayer.bounds.size.height/2)];
        [sublayer setPosition:CGPointMake(_boxLayer.bounds.size.width/2, _boxLayer.bounds.size.height/2)];
        
        [_boxLayer addSublayer: sublayer];
    }
    
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *t = [touches anyObject];
    CGPoint touchPoint = [t locationInView:self];
    [_boxLayer setPosition:touchPoint];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *t = [touches anyObject];
    CGPoint touchPoint = [t locationInView:self];

    // Great trick to avoid delay eg. in case of layer "dragging"
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    [_boxLayer setPosition:touchPoint];
    [CATransaction commit];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(motion == UIEventSubtypeMotionShake)
    {
        self.circleColor = [UIColor redColor];
        [self setNeedsDisplay];
    }
}

- (void)setCircleColor:(UIColor *)circleColor
{
    if(_circleColor != nil)
        _circleColor = nil;
    
    _circleColor = circleColor;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    // Get current draw context
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx); // BRONZE CHALLENGE
    CGRect boundsRect = [self bounds];

    // Figure out of the center of the bounds rectangle
    CGPoint center = CGPointMake(boundsRect.origin.x + boundsRect.size.width / 2,
                                 boundsRect.origin.y + boundsRect.size.height / 2);
    
    // hypot(a, b) - returns sqrt(pow(a,2)+pow(b,2)).
    float maxRadius = hypotf(boundsRect.size.width, boundsRect.size.height)/2;
    
    // Set thickness of the line.
    CGContextSetLineWidth(ctx, 10);
        
    // Add circle shape to the context (this DOES NOT DRAW the shape)
    CGContextAddArc(ctx, center.x, center.y, maxRadius, 0.0, M_PI * 2.0, YES);
    
    // Perform drawing instructions. Draw current shape with current state.
    CGContextStrokePath(ctx);
    
    const float* colors = CGColorGetComponents(_circleColor.CGColor);
    
    for(float currRadius = maxRadius; currRadius > 0; currRadius -= 20)
    {
        
        // BRONZE CHALLENGE
        CGContextSetRGBStrokeColor(ctx, colors[0], colors[1], colors[2], 1.0);
        
        // Add a path to che context
        CGContextAddArc(ctx, center.x, center.y, currRadius, 0.0, M_PI * 2.0, YES);
        
        // Perform drawing instruction. It REMOVES path from context
        CGContextStrokePath(ctx);
    }
    
    // Draw text
    NSString* text = @"You are getting sleepy... ;-)";
    UIFont* f = [UIFont boldSystemFontOfSize:15.0];
    
    CGRect textRect;
    textRect.size = [text sizeWithFont:f];
    textRect.origin.x = center.x - textRect.size.width / 2.0;
    textRect.origin.y = center.y - textRect.size.height / 2.0;
    
    // Set fill color of the current context to black
    [[UIColor blackColor] setFill];
    
    // Draw text shadow
    CGSize offset = CGSizeMake(4.0, 3.0);
    
    CGColorRef color = [[UIColor darkGrayColor] CGColor];
    
    CGContextSetShadowWithColor(ctx, offset, 2.0, color);
    [text drawInRect:textRect withFont:f];
    
    // SILVER CHALLENGE
    CGContextRestoreGState(ctx);    
    CGContextSetShadowWithColor(ctx, offset, 2.0, color);
    
    CGContextSetRGBStrokeColor(ctx, 0.0, 1.0, 0.0, 1.0); 

    CGContextMoveToPoint(ctx, self.center.x - 10.0, self.center.y);
    CGContextAddLineToPoint(ctx, self.center.x + 10.0, self.center.y);
    CGContextStrokePath(ctx);
    
    CGContextMoveToPoint(ctx, self.center.x, self.center.y- 10.0);
    CGContextAddLineToPoint(ctx, self.center.x, self.center.y + 10.0);
    CGContextStrokePath(ctx);
}

@end
